<?php 
namespace Models;
/**
 * 收货地址
 * @package Models
 * @author 江羿平<hugcolin@163.com>
 */
class Address extends Model{

	protected $_table = 'address';

	protected $_filter = FALSE;

	protected $_primekey = 'id';

	protected $_belongs = [
		'province_region' => [
			'class' => Region::Class,
			'foreign' => 'id',
			'belongs_key' => 'province'
		],
		'city_region' => [
			'class' => Region::Class,
			'foreign' => 'id',
			'belongs_key' => 'city'
		],
		'area_region' => [
			'class' => Region::Class,
			'foreign' => 'id',
			'belongs_key' => 'area'
		],
	];

	/**
	 * 获取带地区名称的地址
	 */
	public function region($id){

		$address = $this->find($id);
		if(!$address) return false;
		$region = new Region;
		$regions = $region->where_in('id',array($address['province'],$address['city'],$address['area']))->order_by('type','asc')->get();
		$address['region'] = '';
		foreach ($regions as $r) {
			$address['region'] .= $r['name'];
		}
		return $address;
	}
}
?>